package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.ProductCategoryConverter;
import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.error.SystemCodeEnum;
import com.goods.common.error.SystemException;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.system.PageVO;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * projectName: goods
 *
 * @author: ysc
 * time: 2022/12/2 15:14 周五
 * description:
 */
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {


    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductCategoryMapper productCategoryMapper;


    //查询物资类别列表
    @Override
    public PageVO<ProductCategoryTreeNodeVO> findcategoryTree(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        //获取所有数据
        List<ProductCategory> productCategoryList = productCategoryMapper.selectAll();
        //新建一个节点vo 集合  将转成vo放进集合中
        List<ProductCategoryTreeNodeVO> productVOS = new ArrayList<>();
        //判断获取的数据是否为空
        if (!CollectionUtils.isEmpty(productCategoryList)){
            for (ProductCategory productCategory : productCategoryList){
                //新建一个节点vo
                ProductCategoryTreeNodeVO  p = new ProductCategoryTreeNodeVO();
                //赋值给新建节点
                BeanUtils.copyProperties(productCategory, p);
                //添加到集合中
                productVOS.add(p);
            }
        }
        List<ProductCategoryTreeNodeVO> categoryTreeNodeVOList = CategoryTreeBuilder.build(productVOS);
        PageInfo<ProductCategory> info =new PageInfo<>(productCategoryList);
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = ListPageUtils.page(categoryTreeNodeVOList, pageSize, pageNum);
        return new PageVO<>(info.getTotal(), productCategoryTreeNodeVOList);
    }


    //获取父分类列表信息
    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
        List<ProductCategory> productCategoryList = productCategoryMapper.selectAll();
        //新建一个节点vo 集合  将转成vo放进集合中
        List<ProductCategoryTreeNodeVO> productVOS = new ArrayList<>();
        //判断获取的数据是否为空
        if (!CollectionUtils.isEmpty(productCategoryList)){
            for (ProductCategory productCategory : productCategoryList){
                //新建一个节点vo
                ProductCategoryTreeNodeVO  p = new ProductCategoryTreeNodeVO();
                //赋值给新建节点
                BeanUtils.copyProperties(productCategory, p);
                //添加到集合中
                productVOS.add(p);
            }
        }
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = CategoryTreeBuilder.buildParent(productVOS);
        return productCategoryTreeNodeVOList;
    }


    //添加物资
    @Override
    public void add(ProductCategory productCategory) {
        ProductCategory productCategory1 = new ProductCategory();
        BeanUtils.copyProperties(productCategory,productCategory1);
        productCategory1.setCreateTime(new Date());
        productCategory1.setModifiedTime(new Date());
        productCategoryMapper.insert(productCategory1);
    }


    //编辑物资
    @Override
    public ProductCategory edit(Long id) throws SystemException{
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);
        if (productCategory == null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"编辑的物资不存在");
        }
        return ProductCategoryConverter.converterToProductCategoryVO(productCategory);
    }


    //更新物资
    @Override
    public void update(Long id, ProductCategory productCategory) throws SystemException{
//        Product dbproduct = productMapper.selectByPrimaryKey(id);
//        if (dbproduct == null){
//            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要更新的物资不存在");
//        }
        ProductCategory productCategory1 = new ProductCategory();
        BeanUtils.copyProperties(productCategory, productCategory1);
        productCategory1.setId(id);
        productCategory1.setModifiedTime(new Date());
        productCategoryMapper.updateByPrimaryKeySelective(productCategory1);
    }


    //删除物资
    @Override
    public void delete(Long id) throws SystemException {
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);

        if (productCategory == null ){
            throw  new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要删除的物资不存在");
        }

        productMapper.deleteByPrimaryKey(id);
    }




}
